package ch.fhnw.qualiscan.api;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import ch.fhnw.qualiscan.api.output.BestPracticeOutput;
import ch.fhnw.qualiscan.api.output.BestPracticesOutput;
import ch.fhnw.qualiscan.api.output.LinkOutput;
import ch.fhnw.qualiscan.application.models.ApiModel;
import ch.fhnw.qualiscan.core.db.pojos.BestPractice;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.Survey;
import ch.fhnw.qualiscan.core.db.pojos.University;
import ch.fhnw.qualiscan.core.db.pojos.User;

@Path("/bestpractices")
public class BestPracticeAPI {

	@Context
    protected UriInfo uriInfo;

    @Context
    protected Request request;
	
    ApiModel model;
    Response response = null;
    
	public BestPracticeAPI() {
		model = new ApiModel();
	}
	
	@GET
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response getBestPractices(@QueryParam("start") @DefaultValue("0") int start,
            @QueryParam("size") @DefaultValue("20") int size,
            @QueryParam("sort") @DefaultValue("id") String sort,
            @QueryParam("sortType") @DefaultValue("DESC") String sortType,
            @QueryParam("universities") @DefaultValue("") String universities,
            @QueryParam("domains") @DefaultValue("") String domains,
            @QueryParam("surveys") @DefaultValue("") String surveys,
            @QueryParam("users") @DefaultValue("") String users) {
		
		// generate survey list
		List<Survey> surveyList = new ArrayList<Survey>();
		if(!surveys.equals("")) {
			String[] arr = surveys.split(";");
			for(int i = 0; i < arr.length; i++) {
				int id;
				try {
					id = Integer.parseInt(arr[i]);
				} catch(NumberFormatException e) {
					break;
				}
				Survey survey = model.getSurvey(id);
				surveyList.add(survey);
	        }
		} else {
			surveyList = null;
		}
		
		// generate domain list
		List<Domain> domainList = new ArrayList<Domain>();
		if(!domains.equals("")) {
			String[] arr = users.split(";");
			for(int i = 0; i < arr.length; i++) {
				int id;
				try {
					id = Integer.parseInt(arr[i]);
				} catch(NumberFormatException e) {
					break;
				}
				Domain domain = model.getDomain(id);
				domainList.add(domain);
	        }
		} else {
			domainList = null;
		}
		
		// generate user list
		List<User> userList = new ArrayList<User>();
		if(!users.equals("")) {
			String[] arr = users.split(";");
			for(int i = 0; i < arr.length; i++) {
				int id;
				try {
					id = Integer.parseInt(arr[i]);
				} catch(NumberFormatException e) {
					break;
				}
				User user = model.getUser(id);
				userList.add(user);
	        }
		} else {
			userList = null;
		}
	
		// generate university list
		List<University> universityList = new ArrayList<University>();
		if(!universities.equals("")) {
			String[] arr = users.split(";");
			for(int i = 0; i < arr.length; i++) {
				int id;
				try {
					id = Integer.parseInt(arr[i]);
				} catch(NumberFormatException e) {
					break;
				}
				University uni = model.getUniversity(id);
				universityList.add(uni);
	        }
		} else {
			universityList = null;
		}
		
		List<BestPractice> allBPs = model.getBestPractices(surveyList, domainList, userList, universityList, sort, sortType, true, start, size);
		List<LinkOutput> links = Linker.getBestPracticesLinks(start, size, sort, sortType, universities, surveys, domains, users, uriInfo.getAbsolutePathBuilder(), allBPs.size());
		
        if(allBPs == null) {
           response = Response.noContent().build();
           return response;
        }
        
        BestPracticesOutput bpOutput = new BestPracticesOutput(allBPs, links);
        response = Response.ok(bpOutput).build();
        
        return response;
    }
	
	@GET
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @Path("{id}")
    public Response getBestPractice(@PathParam("id") int id) {

        BestPractice bestPractice = model.getBestPractice(id);

        if(bestPractice == null) {
           response = Response.noContent().build();
           return response;
        }
        
        BestPracticeOutput bpO = new BestPracticeOutput(bestPractice);
        response = Response.ok(bpO).build();
        
        return response;
	}
	
}
